***Note: replicators should set directory to open data 
*cd ""


********** CLEANING DATA **********

* PART 3: Define all variables used in individual-level regressions

 clear all
 use  Data.dta
 

* (a) Independent variables


* Here the coding  needs to (i) harmonize again across different ESS waves, (ii) clean and rename into intuitive labels, (iii) label for graphs, (iv) recode to ease interpretation

rename agea Age
label var Age "Age"
drop if Age<19
replace hinctnt=10 if hinctnt>10
replace hinctnta=hinctnt if hinctnta==. | hinctnta==.a | hinctnta==.b | hinctnta==.c | hinctnta==.d & essround<5
rename hinctnta Income2
la var Income2 "Household income"

gen Age_bracket=.
replace Age_bracket=0 if Age>14 & Age<=24
replace Age_bracket=1 if Age>25 & Age<=49
replace Age_bracket=2 if Age>50 & Age<=64
replace Age_bracket=3 if Age>65 & Age<=80
replace Age_bracket=4 if Age>80
la var Age_bracket "Age"
la def wawa 0 "15-24" 1 "25-49" 2 "50-64" 3 "65-80" 4 "80+", modify
la val Age_bracket wawa

 
gen Age2=Age*Age
la var Age2 "Age, squared"

gen Income=4-hincfel
la def oui  0 "Very hard to cope"  1 "Hard to cope" 2 "Coping fine" 3 "Living comfortably", modify
la val Income oui
la var Income "Subjective income"

rename uempla Unemployed
la var Unemployed "Unemployed"
la def w 0 "No" 1 "Yes", modify
la val Unemployed w

rename hhmmb Household
la var Household "Household size"
 
gen Child=chldhm==1 if chldhm!=.
la var Child "Household status"
la def d 1 "Lives with children" 2 "Does not", modify
la val Child d

gen Rural=domicil>=3 if domicil!=.
rename domicil Domicile
la var Domicile "Domicile"
la def domus 1 "Big city" 2 "Suburbs" 3 "Town" 4 "Village" 5 "Countryside", modify
la val Domicile domus
 

g eduyrs2=eduyrs*eduyrs
la var eduyrs2 "Years of completed education, squared"
la var eduyrs "Years of completed education"

gen Immigrant=brncntr==2 if brncntr!=.
la var Immigrant "Immigrant"
la def toto 0 "Born in country" 1 "Born abroad", modify
la val Immigrant toto


*There's some more recoding/cleaning going on with all education related variables because they are used in the appendix

gen Education=edulvla-1 if edulvla!=55 
la def z 0 "Primary" 1 "Lower secondary" 2 "Upper secondary" 3 "Vocational" 4 "Tertiary", modify
la val Education z
replace Education=0 if eisced==1 & essround==9
replace Education=1 if eisced==2 & essround==9
replace Education=2 if eisced==3 & essround==9
replace Education=2 if eisced==4 & essround==9
replace Education=3 if eisced==5 & essround==9
replace Education=4 if eisced==6 & essround==9
replace Education=4 if eisced==7 & essround==9


replace eiscedf=. if eiscedf==55
replace eiscedm=. if eiscedm==55
replace edulvlfa=. if edulvlfa==55
replace edulvlma=. if edulvlma==55

 
gen Edu_dad=.
replace Edu_dad=0 if edulvlfa<=2 & essround!=9
replace Edu_dad=0 if eiscedf<=2 & essround==9
replace Edu_dad=1 if edulvlfa>2 & essround!=9
replace Edu_dad=1 if eiscedf>2 & essround==9 

gen Edu_mum=.
replace Edu_mum=0 if edulvlma<=2 & essround!=9
replace Edu_mum=0 if eiscedm<=2 & essround==9
replace Edu_mum=1 if edulvlma>2 & essround!=9
replace Edu_mum=1 if eiscedm>2 & essround==9 


recode edulvlfa (0=.) (55=.)
rename edulvlfa Education_dad
la var Education_dad "Father's education"
recode edulvlma (0=.) (55=.)
rename edulvlma Education_mum
la var Education_mum "Mother's education"

 
replace total_eduyrs=18 if total_eduyrs==18.5
replace total_eduyrs=14 if total_eduyrs==14.5
la var total_eduyrs "Years of education"
la var Edu_mum "Maternal education"

gen total_eduyrs2=total_eduyrs
la var total_eduyrs2 "Years of education"
replace eduyrs=eduyrs_actual
 
 

egen clusterV=group(essround Country)
rename treatment T
la var T "Treatment"
rename running r
la var r "Running"

*These are variables used in appendix for descriptives

gen Interest=4-polintr
label var Interest "Interest in Politics"

rename lrscale Ideology
label var Ideology "Left - Right placement"

iscogen  Job = siops(iscoco) ,  from(isco88)   
iscogen  Job2 = siops(isco08) ,  from(isco08)   
replace Job=Job2 if Job==.
la var Job "Occupational prestige"
	
	gen Turnout=0
	replace Turnout=1 if vote==1
	gen Contact=2-contplt
	gen Sign=2-sgnptit
	gen Protest=2-pbldmn
	gen Badge=2-badge
	gen Vote=Turnout
	gen Boycott=2-bctprd
gen Freedom=5-impfree
gen Authority=5-ipfrule
gen Meritocracy=5-ipsuces
gen Tradition=5-imptrad
gen Equality=5-ipeqopt
	rename trstplt Politicians
	rename trstprl Parliament  
	rename trstlgl Judicial 
	rename trstplc Police 
	rename trstprt Parties
	rename stfdem Democracy
 
 
label var stfgov "Government approval"
label var Parliament "Institutional trust: Parliament"
label var Police "Institutional trust: Police"
label var Judicial "Institutional trust: Judicial"
label var Politicians "Institutional trust: Politicians"
label var Parties "Institutional trust: Parties"
label var Democracy "Institutional trust: Democracy"
label var Vote "Vote"
label var Ideology "Left - right scale"

 gen total_recoded= 0 if total_eduyrs<=10
 replace total_recoded=1 if total_eduyrs>10 & total_eduyrs<=12
 replace total_recoded=2 if total_eduyrs>13 & total_eduyrs<=15
 replace total_recoded=3 if total_eduyrs>15

encode country, gen(Country)
tabulate essround, generate(r)
tabulate Country, generate(c)


* (b) Dependent variables


*Discriminatory attitudes

*Ethnoracial 

*Main

gen   D_laws=10-lwdscwp
rename imdfetn Immigration_ethnic
rename imsmetn Immigration_same
g 	D_race=D_laws/10
la var D_race "Discriminatory ethnoracial attitudes"


*Robustness

gen D_race2=.
replace D_race2=0 if Immigration_same==Immigration_ethnic
replace D_race2=1 if Immigration_ethnic>Immigration_same
 la var D_race2 "Discriminatory ethnoracial attitudes (binary)"

 replace D_race=. if Ingroup==0
replace D_race2=. if Ingroup==0

*Gender

*Main 
 
gen D_gender=(5-mnrgtjb)/4 if Female==0
la var D_gender "Discriminatory gender attitudes"
 
*Robustness


*Perceptions of Discrimination

gen PD_race=0 
replace PD_race=1 if dscrrce==1 | dscretn==1  
replace PD_race=. if Ingroup==1 
la var PD_race "Perceptions of ethnoracial discrimination"


gen PD_gender=0
replace PD_gender=1 if dscrgnd==1 & Female==1
replace PD_gender=. if Female==0
la var PD_gender "Perceptions of gender discrimination"

 drop if treatment==.

 
 
 /* Run first stage eligibility here to construct what follows and to retrieve F-stats as appearing in appendix table
 
levelsof reform, local(levels) 

 foreach x of local levels{
	di "`x'"
rdrobust eduyrs_actual r if reform=="`x'", p(1) 
rdbwselect  eduyrs_actual r if reform=="`x'", p(1) 
local u1 =e(h_mserd)
g w=max(0,`u1'-abs(r)) 
reg eduyrs_actual r [pweight=w]  if reform=="`x'", robust
display %8.2fc e(F)
display e(N)
drop w
}	
  */
  

  
*Countries with monotonic first stage 
 
gen Monotonic=0
replace  Monotonic=1 if cntry=="AT"  | cntry=="BE" | cntry=="CH"  | cntry=="DE"  | cntry=="DK" | cntry=="FI" |  cntry=="FR" | cntry=="GB" | cntry=="ES"   | cntry=="GR"    | cntry=="IE" | cntry=="IS" | cntry=="NL"  | cntry=="NO"  | cntry=="PT" | cntry=="SE"  | cntry=="RU" | cntry=="CZ"        


*Countries with strong first stage 

gen Strong=0
replace Strong=1 if cntry=="AT" | cntry=="DK" | cntry=="FR" | cntry=="GB" | cntry=="NL" | cntry=="LT"
 
 
* Done with cleaning

save   Final.dta,replace
 
 
 

